<template>
 <div>
   <div class="bgfff mtrbl20 ptrbl20 nopb20">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item  prop="companyAdminId">
          <el-cascader style="width:300px" clearable v-model="companyAdminId" :options="companyAdminTwoCascade" :show-all-levels="false"  placeholder="请选择管理企业"></el-cascader>
        </el-form-item>
        <el-form-item prop="month">
            <el-date-picker
            v-model="queryParams.month"
            style="width: 170px"
            value-format="yyyyMM"
            type="month"
            placeholder="请选择完税时间">
            </el-date-picker>
        </el-form-item>

        <el-form-item prop="action">
          <el-select
            v-model="queryParams.action"
            placeholder="请选择开票金额"
            style="width:150px"
            clearable
          >
            <el-option label="小于" value="0"></el-option>
            <el-option label="大于" value="1"></el-option>
            <el-option label="范围" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="standardMoney" v-if="queryParams.action == 0 || queryParams.action == 1">
          <el-input
            v-model="standardMoney"
            placeholder="请输入金额"
            clearable
        
            style="width: 150px"
          /> <span class="mr20">万元</span>
        </el-form-item>

        <el-form-item prop="minMoney"  v-if="queryParams.action == 2">
          <el-input
            v-model="minMoney"
            placeholder="最低金额"
            clearable
    
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item v-if="queryParams.action == 2">-</el-form-item>
        <el-form-item prop="maxMoney" v-if="queryParams.action == 2">
          <el-input
            v-model="maxMoney"
            placeholder="最高金额"
            clearable
            style="width: 120px"
          /> <span class="mr20">万元</span>
        </el-form-item>

        <el-form-item prop="invoiceStatus">
            <el-select
              v-model="queryParams.invoiceStatus"
              placeholder="开票状态"
              clearable
              style="width: 150px"
            >
              <el-option label="未开票" value="0"></el-option>
              <el-option label="开票中" value="1"></el-option>
              <el-option label="部分未开票" value="2"></el-option>
              <el-option label="已开票" value="3"></el-option>
              <el-option label="已补录" value="4"></el-option>
              <el-option label="不予开票" value="5"></el-option>
              <el-option label="开票失败" value="6"></el-option>
              <el-option label="开票数据异常" value="7"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="isAppend">
          <el-select
            v-model="queryParams.isAppend"
            placeholder="是否筛选补开票"
            clearable
            style="width: 150px"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="taxIdentity">
          <el-select
            v-model="queryParams.taxIdentity"
            placeholder="纳税人类型"
            clearable
            multiple
            collapse-tags
            style="width: 180px"
          >
            <el-option label="个体工商户" :value="1"></el-option>
            <el-option label="临时税务登记" :value="0"></el-option>
            <el-option label="自然人" :value="88"></el-option>
          </el-select>
        </el-form-item>
          <!-- <el-form-item prop="declareStatus">
            <el-select
              v-model="queryParams.declareStatus"
              placeholder="申报状态"
              clearable
              style="width: 130px"
            >
              <el-option label="未申报" value="0"></el-option>
              <el-option label="部分申报" value="2"></el-option>
              <el-option label="已申报" value="3"></el-option>
            </el-select>
          </el-form-item> -->
      </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
         </el-form-item>
         <el-form-item>
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="medium"
              @click="handleBatchExport"
              v-hasPermi="['taxmsg:invoice:dataDownload']"
            >导出数据</el-button>
            <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="medium"
            @click="handleImport"
            v-hasPermi="['taxmsg:invoice:importUpdateData']"
          >导入数据</el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-view"
            size="medium"
            @click="handleImportHistory"
            v-hasPermi="['taxmsg:invoice:importListBatch']"
          >查看导入历史</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb20">
          <el-col :span="1.5">
          </el-col>
          <el-col :span="1.5">
          </el-col>
          <el-col :span="1.5">
          </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->
        </el-col>
      </el-row>
  </div>      
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-table v-loading="loading" :data="invoiceList">
          <el-table-column fixed label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="left" key="realName" prop="realName"/>
          <el-table-column label="证件号" align="left" key="idNumber" prop="idNumber" width="180px"/>
          <el-table-column label="手机号" align="left" key="phone" prop="phone" />
          <el-table-column label="个人户名称" align="left" key="enterpriseName" prop="enterpriseName" width="200px" :show-overflow-tooltip="true"/>
          <el-table-column label="统一社会信用代码" align="left" key="taxNo" prop="taxNo" width="250" :show-overflow-tooltip="true"/>
          <el-table-column label="应开金额" align="left" key="totalMoney" prop="totalMoney" />
          <el-table-column label="实开金额" align="left" key="actualMoney" prop="actualMoney" />
          <el-table-column label="开票日期" align="left" key="appendDate" prop="appendDate" />
          <el-table-column label="补票日期" align="left" key="taxDate" prop="taxDate" /> 

          <el-table-column label="开票状态" align="left" key="invoiceStatus.name" prop="invoiceStatus.name" />
          <el-table-column label="开票省份" align="left" key="provinceFullName" prop="provinceFullName" />
          <el-table-column label="管理企业" align="left" key="companyAdminName" prop="companyAdminName" width="200"/>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
              <el-dropdown size="mini" split-button type="primary" @click="handleView(scope.row)" placement="bottom-start" trigger="click">
                详 情
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-edit" @click.native="handleEdit(scope.row)" v-hasPermi="['taxmsg:invoice:invoiceUpdate']">编辑状态</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-upload2" @click.native="handleUpload(scope.row)" v-hasPermi="['taxmsg:invoice:fileUpload']">上传发票</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-download" @click.native="handleDownload(scope.row)" v-hasPermi="['taxmsg:invoice:fileList']">下载发票</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleUpload(scope.row)"
            >上传发票</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleDownload(scope.row)"
            >下载发票</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- Excel导入修改申报数据对话框 -->
    <el-dialog :title="upload1.title" :visible.sync="upload1.open" width="400px" append-to-body>
      <el-upload
        ref="upload1"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload1.headers"
        :action="upload1.url"
        :disabled="upload1.isUploading"
        :on-progress="handleFileUploadProgress1"
        :on-success="handleFileSuccess1"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm1">确 定</el-button>
        <el-button @click="upload1.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 上传发票对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept="*"
        :headers="upload.headers"
        :action="upload.url"
        :data="{invoiceId:this.form.invoiceId,batchId:this.form.batchId,remark:this.remark}"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <!-- <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div> -->
        <!-- <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div> -->
      </el-upload>
      <el-form class="mt20">
        <el-form-item prop="remark">
          <el-input type="textarea" v-model="remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编辑状态对话框 -->
    <el-dialog :title="title" :visible.sync="openInvoiceUpdate" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="开票状态" prop="invoiceStatus">
          <el-select
            v-model="form.invoiceStatus"
            placeholder="全部"
            clearable
            size="small"
          >
            <el-option label="已开票" value="3"></el-option>
            <el-option label="开票中" value="1"></el-option>
            <el-option label="开票失败" value="6"></el-option>
            <el-option label="不予开票" value="5"></el-option>
            <el-option label="已补录" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开票金额" prop="actualMoney">
          <el-input style="width: 215px" v-model="form.actualMoney" clearable placeholder="请输入开票金额" />
        </el-form-item>
        <el-form-item label="补开票年月" v-if="form.invoiceStatus == 4" prop="appendDate">
            <el-date-picker
            v-model="form.appendDate"
            size="large"
            style="width: 215px"
            value-format="yyyyMM"
            type="month"
            placeholder="补开票年月">
            </el-date-picker>
         </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelInvoiceUpdate">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 开票资料列表对话框 -->
    <el-dialog title="开票资料列表" :visible.sync="openInvoiceFileList" width="1100px" append-to-body>
      <el-table v-loading="loading" :data="invoiceFileList">
        <el-table-column label="编号" align="left" key="id" prop="id" width="55"/>
        <el-table-column label="文件名" align="left" key="fileName" prop="fileName" width="200"/>
        <el-table-column label="文件后缀" align="left" key="fileSuffix" prop="fileSuffix"/>
        <el-table-column label="文件大小（kb）" align="left" key="fileSize" prop="fileSize"/>
        <el-table-column label="上传人名称" align="left" key="createByName" prop="createByName"/>
        <el-table-column label="上传时间" align="left" key="createTime" prop="createTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownloadFile(scope.row)"
          >下载发票</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelInvoiceFileList">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
import { invoiceList, invoiceDataDownload, invoiceUpdate, invoiceUpdateAppend, invoiceFileList, companyAdminTwoCascade } from "@/api/taxmsg/invoice";
import { getToken } from "@/utils/auth";
export default {
  name: "InvoiceDetails",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 用户表格数据
      invoiceList: null,
      //开票资料列表
      invoiceFileList: null,
      props: { multiple: true },
      // 构造预申报数据级联条件
      companyAdminTwoCascade: [],
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（编辑状态）
      openInvoiceUpdate: false,
      //是否显示弹出框（开票资料列表）
      openInvoiceFileList: false,
      form: {},
      remark:'',
      // Excel导入修改开票数据
      upload1: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization:getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/tax/invoice/import/update/data"
      },
      // 上传发票参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization:getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/tax/invoice/file/upload"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        month: undefined,
        action: undefined,
        // standardMoney: undefined,
        // minMoney: undefined,
        // maxMoney: undefined,
        invoiceStatus: undefined,
        isAppend: undefined,
        taxIdentity: undefined,
        batchId: this.$route.params && this.$route.params.id
      },
      companyAdminId:null,
      standardMoney:undefined,
      minMoney:undefined,
      maxMoney:undefined,
      // 编辑状态表单校验
      rules: {
        invoiceStatus: [
          { required: true, message: "请选择开票状态", trigger: "blur" }
        ],
        actualMoney: [
          { required: true, message: "实际开票金额不能为空", trigger: "blur" }
        ],
        appendDate: [
          { required: true, message: "补开票目标日期不能为空", trigger: "blur" }
        ]
      }
     
    };
  },
  created() {
    this.getList();
    companyAdminTwoCascade().then(response => {
        this.companyAdminTwoCascade = response.data;
      }
    );
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      if(this.companyAdminId){
        const companyAdminId = this.companyAdminId.map(item => item[0]).slice(1).toString();
        this.queryParams.companyAdminId = companyAdminId;
      }
      
      if(this.standardMoney){
        const standardMoney = this.standardMoney * 10000;
        this.queryParams.standardMoney = standardMoney;
      }else{
         delete this.queryParams.standardMoney;
      }
      
      if(this.minMoney){
        const minMoney = this.minMoney * 10000;
        this.queryParams.minMoney = minMoney;
      }else{
         delete this.queryParams.minMoney;
      }

      if(this.maxMoney){
        const maxMoney = this.maxMoney * 10000;
        this.queryParams.maxMoney = maxMoney;
      }else{
         delete this.queryParams.maxMoney;
      }
      if(this.queryParams.action==""){
        this.queryParams.action = null
      }
      if(this.queryParams.invoiceStatus==""){
        this.queryParams.invoiceStatus = null
      }
      
      invoiceList(this.queryParams).then(response => {
          this.invoiceList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      ).catch(()=>{});
    },
    /** 修改按钮操作 */
    handleView(row) {
      const id = row.id || this.ids[0];
      this.$router.push("/invoice/details2/" + id);
    },
    handleImportHistory() {
      this.$router.push("/invoice/importHistory");
    },
    cancelInvoiceUpdate() {
      this.openInvoiceUpdate = false;
      this.reset();
    },
    reset() {
      this.form = {
        invoiceStatus: undefined,
        actualMoney: undefined,
        appendDate: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 编辑状态按钮操作 */
    handleEdit(row) {
      this.reset();
      this.form.invoiceId= row.id;

      if(row.invoiceStatus.name=="已开票"){
        this.form.invoiceStatus = "3";
      }else if(row.invoiceStatus.name=="开票中"){
        this.form.invoiceStatus = "1";
      }else if(row.invoiceStatus.name=="开票失败"){
        this.form.invoiceStatus = "6";
      }else if(row.invoiceStatus.name=="不予开票"){
        this.form.invoiceStatus = "5";
      }else if(row.invoiceStatus.name=="已补录"){
        this.form.invoiceStatus = "4";
      }

      this.openInvoiceUpdate = true;
      this.title = "编辑";
    },
    cancelInvoiceFileList() {
      this.openInvoiceFileList = false;
    },
    handleDownload(row) {
      //this.form.invoiceId= row.id;
      this.openInvoiceFileList = true;
      this.loading = true;
      invoiceFileList(row.id).then(response => {
          this.invoiceFileList = response.data;
          this.loading = false;
        }
      );
    },
    handleDownloadFile(row) {
      this.getTaxOssAliyunSts(row.fileKey);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.invoiceStatus!=4){
            invoiceUpdate(this.form).then(response => {
              this.msgSuccess(response.msg);
              this.openInvoiceUpdate = false;
              this.getList();
            });
          }else{
            invoiceUpdateAppend(this.form).then(response => {
              this.msgSuccess(response.msg);
              this.openInvoiceUpdate = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.companyAdminId= [],
      this.standardMoney= [],
      this.minMoney= [],
      this.maxMoney= [],
      this.handleQuery();
    },
    // 取消按钮 
    cancel() {
      this.open = false;
    },
    /** 导出按钮操作 */
    handleBatchExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return invoiceDataDownload(queryParams);
        }).then(response => {
          //this.download(response.msg);
          var list_name = "批量发票数据";
          this.export_excel_file(response, list_name);
        }).catch(()=>{})
    },
    /** 上传发票 */
    handleUpload(row) {
      this.form.invoiceId= row.id;
      this.form.batchId = row.batchId;
      this.upload.title = "上传发票";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      //alert(response.code);
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

     /*************************** 导入修改开票数据 **********************************/
    handleImport() {
      this.upload1.title = "导入修改开票数据";
      this.upload1.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress1(event, file, fileList) {
      this.upload1.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess1(response, file, fileList) {
      this.upload1.open = false;
      this.upload1.isUploading = false;
      this.$refs.upload1.clearFiles();
      //alert(response.code);
      this.$alert(response.data.message, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm1() {
      this.$refs.upload1.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      window.location.href = process.env.VUE_APP_BASE_API + "/tax/public/excel/taxInvoiceImportTpl.xlsx";
    },
  }
};
</script>
<style scoped lang="scss">
</style>